package com.hqyj.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.pojo.Order;
import com.hqyj.pojo.User;
import com.hqyj.serviceInf.OrderServiceInf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderServiceInf orderServiceInf;

    // 分页 全查
    @RequestMapping("/getOrder")
    @ResponseBody
    public Map<String,Object> getAllOrder(@RequestParam(defaultValue = "1", required = false)int pageNum,
                                          @RequestParam(defaultValue = "3", required = false)int pageSize ,
                                          @RequestParam(defaultValue = "")String username){
        return orderServiceInf.selectAll(pageNum, pageSize,username);
    }
    //查询一个
    @RequestMapping("/showOrder")
    public String showOrder(int orderid, Model model){
        Order order = orderServiceInf.selecOrderByOrderid(orderid);
        model.addAttribute("order",order);
        return "order_update";
    }
    //添加
    @RequestMapping("/addOrder")
    @ResponseBody
   public String addOrder(@RequestBody Order order){
        return orderServiceInf.insertOrder(order)>0?"redirect:getOrder":"order_add";
    }
    //修改
    @RequestMapping("/updateOrder")
    @ResponseBody
    public Map<String, Object> updateOrder(@RequestBody Order order){
        System.out.println(order);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", -1);
        map.put("message", "修改失败");
        if(orderServiceInf.updateOrder(order) > 0){
            map.put("code", 200);
            map.put("message", "修改成功");
            map.put("url", "/order/getOrder");
        }
        return map;
    }
    //删除
    @RequestMapping("/deleteOrder")
    public String deleteOrder(@RequestParam("orderid") int orderid){
        return orderServiceInf.deleteOrder(orderid)>0?"redirect:getOrder":"error";
    }
    //页面通用跳转方法
    @RequestMapping("{page}")
    public String toPage(@PathVariable()String page){
        return page;
    }

}
